package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.FlyMessageDao;
import com.kun.portal.webframe.po.AppointmentPo;
import com.kun.portal.webframe.po.FlyMessagePo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/5/4.
 */
@Service
public class FlyMessageService {
    @Autowired
    private FlyMessageDao flyMessageDao;
    @Autowired
    private EntityManager entityManager;
    public FlyMessagePo save(FlyMessagePo flyMessagePo) {
        return  this.flyMessageDao.save( flyMessagePo);
    }

    public Page<FlyMessagePo> query(String filter,Pageable pageable,Date startData,Date endDate,String status){

            Specification query = new Specification<AppointmentPo>() {
                @Override
                public Predicate toPredicate(Root<AppointmentPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Predicate predicate =null;
                    if(StringUtils.isNoneBlank(filter)){
                        List<Predicate> predicates = new ArrayList<>();
                        predicates.add(cb.like(root.get("description"), "%"+filter+"%"));
                        predicates.add(cb.like(root.get("name"), "%"+filter+"%"));
                        predicates.add(cb.like(root.get("bakName"), "%"+filter+"%"));
                        predicate = cb.or(predicates.toArray(new Predicate[predicates.size()]));
                    }
                    if(startData!=null){
                        if(predicate!=null){
                            predicate = cb.and(cb.greaterThanOrEqualTo(root.get("messageDate"),startData),predicate);
                        }
                        else{
                            predicate=cb.greaterThanOrEqualTo(root.get("messageDate"),startData);
                        }

                    }
                    if(endDate!=null){
                        if(predicate!=null){
                            predicate = cb.and(cb.lessThanOrEqualTo(root.get("messageDate"),endDate),predicate);
                        }else{
                            predicate = cb.lessThanOrEqualTo(root.get("messageDate"),endDate);
                        }
                    }
                    if(StringUtils.isNoneBlank(status)){
                        if(predicate!=null){
                            predicate = cb.and(cb.equal(root.get("status"),status),predicate);
                        }else{
                            predicate = cb.equal(root.get("status"),status);
                        }

                    }
                    return  predicate;
                }
            };
            return this.flyMessageDao.findAll(query,pageable);
    }
}
